package com.wmyg.scanninggun.service.impl;

import com.alibaba.fastjson.JSON;
import com.wmyg.scanninggun.bean.BillNew;
import com.wmyg.scanninggun.dao.BillNewMapper;
import com.wmyg.scanninggun.dao.DataPreMapper;
import com.wmyg.scanninggun.service.PackageNewService;
import com.wmyg.scanninggun.utils.BillConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class PackageNewServiceImpl implements PackageNewService {

    @Autowired
    BillNewMapper billNewMapper;

    @Autowired
    DataPreMapper dataPreMapper;

    @Autowired
    AmqpTemplate amqpTemplate;

    @Override
    public boolean pack(BillNew billNew) {
        long start = System.currentTimeMillis();
        List<BillNew> newRelevants = new ArrayList<>();
        List<BillNew> oldRelevants = new ArrayList<>();
        List<BillNew> tmp  = billNewMapper.selectAllByNewBill(billNew);

        tmp.forEach((b) -> {
            if (b.getCreatime().after(billNew.getCreatime())) {
                newRelevants.add(b);
            } else {
                oldRelevants.add(b);
            }
        });
        boolean isValid = (newRelevants.size()==0);
        long end1 = System.currentTimeMillis();
        log.info("NEW-----Pack Service Before insert async spend {}ms,", end1 - start);
        insertBill(billNew, oldRelevants, isValid);
        long end2 = System.currentTimeMillis();
        log.info("NEW-----Pack Service Before insert async spend {}ms, after spend{}ms", end1 - start, end2 - start);
        return isValid;
    }

    @Override
    @Transactional
    public void insertBill(BillNew billNew, List<BillNew> oldRelevants, boolean isValid) {
        System.out.println("sssssssssssssssssssssssss");
        if (isValid) {
            Boolean flag = true;
            long start = System.currentTimeMillis();
            String prefix = "(01)" + billNew.getGtin() + "(10)" + billNew.getBatch() + "(21)";
            if (dataPreMapper.select(prefix + billNew.getCaseId()) < 1) {
                flag = false;
            } else {
                for (String boxId :
                        billNew.getBoxes()) {
                    if (dataPreMapper.select(prefix + boxId) < 1) {
                        flag = false;
                        break;
                    }
                }
            }
            long end = System.currentTimeMillis();
            log.info("NEW-----bill check spends {}ms", end - start);
            if (flag) {
                billNew.setState(3);

            } else {
                billNew.setState(1);
            }
        } else {
            billNew.setState(2);
        }
        log.info("NEW-----The bill state is {}", billNew.getState());
        billNewMapper.insert(billNew);
        billNewMapper.batchInsertBox(billNew);
        oldRelevants.forEach((b) -> {
            b.setState(2);
            billNewMapper.update(b);
        });
        String billJson = JSON.toJSONString(BillConverter.billNew2Bill(billNew));
        amqpTemplate.convertAndSend("bill-upload", billJson);
    }

    @Override
    public int selectValidCount(String deviceId, String startTime) {
        return billNewMapper.selectValidCount(deviceId, startTime);
    }

    @Override
    public int selectInvalidCount(String deviceId, String startTime) {
        return billNewMapper.selectInvalidCount(deviceId, startTime);
    }
}
